// --------------------------------------------------------------------------------------------------------------------
// <copyright file="DC.Instruments.cs" company="Open Trader">
//   Copyright (c) David Denis (david.denis@systemathics.com)
// </copyright>
// <summary>
//   |  Open Trader - The Open Source Systematic Trading Platform
//   |
//   |  This program is free software: you can redistribute it and/or modify
//   |  it under the terms of the GNU General Public License as published by
//   |  the Free Software Foundation, either version 2 of the License, or
//   |  (at your option) any later version.
//   |
//   |  This program is distributed in the hope that it will be useful,
//   |  but WITHOUT ANY WARRANTY; without even the implied warranty of
//   |  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   |  GNU General Public License for more details.
//   |
//   |  You should have received a copy of the GNU General Public License
//   |  along with this program.  If not, see http://www.gnu.org/licenses
//   |
//   |  Up to date informations about Open Trader can be found at :
//   |    http://opentrader.org
//   |    http://opentrader.codeplex.com
//   |
//   |  For professional services, please visit us at :
//   |    http://www.systemathics.com
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Org.OpenTrader.Framework.Forge.DataCache
{
    #region Usings

    using System;
    using System.Collections.Generic;
    using System.Linq;

    using Org.OpenTrader.Framework.Forge.DataModel;
    using Org.OpenTrader.Framework.Forge.Enums;
    using Org.OpenTrader.Framework.Forge.Interfaces;

    #endregion

    /// <summary>
    /// The DAO for instrumentsCache. 
    /// Caching is implemented for all methods present here.
    /// </summary>
    public partial class DC
    {
        #region Constants and Fields

        /// <summary>
        /// The instrumentsCache.
        /// </summary>
        private readonly IDictionary<Guid, IInstrumentStaticData> instrumentsCache = new Dictionary<Guid, IInstrumentStaticData>();

        /// <summary>
        /// True if the instrumentsCache has all been loaded
        /// </summary>
        private bool instrumentsCacheFullyLoaded;

        #endregion

        #region Public Methods

        /// <summary>
        /// The create instrument static data.
        /// </summary>
        /// <param name="marketId">
        /// The marketId.
        /// </param>
        /// <param name="tickSizeRuleId">
        /// The tickSizeRuleId.
        /// </param>
        /// <param name="instrumentType">
        /// The instrumentType.
        /// </param>
        /// <param name="instrumentName">
        /// The instrumentName.
        /// </param>
        /// <param name="instrumentCode">
        /// The instrumentCode.
        /// </param>
        /// <param name="instrumentIsin">
        /// The instrumentIsin.
        /// </param>
        /// <param name="instrumentCurrency">
        /// The instrumentCurrency.
        /// </param>
        /// <param name="tradingScope">
        /// The tradingScope.
        /// </param>
        /// <returns>
        /// </returns>
        public IInstrumentStaticData CreateInstrumentStaticDataAndCache(
            Guid marketId, 
            Guid tickSizeRuleId, 
            string instrumentType, 
            string instrumentName, 
            string instrumentCode, 
            string instrumentIsin, 
            ECurrency instrumentCurrency, 
            ETradingScope tradingScope)
        {
            var i = DBInstrumentStaticDataHelpers.CreateInstrumentStaticData(
                marketId, tickSizeRuleId, instrumentType, instrumentName, instrumentCode, instrumentIsin, instrumentCurrency, tradingScope);
            this.instrumentsCache[i.ID] = i;
            return i;
        }

        /// <summary>
        /// The load instrumentsCache.
        /// </summary>
        /// <param name="dictionaryId">
        /// The dictionaryId.
        /// </param>
        /// <returns>
        /// </returns>
        public IInstrumentStaticData[] GetInstrumentsStaticDataFromCache(Guid dictionaryId)
        {
#if BENCHMARKDAOINSTRUMENTS
            var v0 = Timings.BenchmarkNow;
#endif

            // If cache is not loaded, load it
            if (!this.instrumentsCacheFullyLoaded)
            {
                // Fill the cache
                foreach (var i in DBInstrumentStaticDataHelpers.LoadInstrumentsStaticData(dictionaryId))
                {
                    this.instrumentsCache[i.ID] = i;
                }

                // FieldCache fully loaded
                this.instrumentsCacheFullyLoaded = true;
            }

            // Convert to array
            var allinstruments = this.instrumentsCache.Values.ToArray();

#if BENCHMARKDAOINSTRUMENTS

    // Benchmarking
            Timings.Benchmark("GetInstrumentsStaticDataFromCache", v0);
#endif

            // Return array
            return allinstruments;
        }

        /// <summary>
        /// The get instrument.
        /// </summary>
        /// <param name="instrumentID">
        /// The instrumentID.
        /// </param>
        /// <typeparam name="T">
        /// </typeparam>
        /// <returns>
        /// </returns>
        public T GetInstrumentStaticDataFromCache<T>(Guid instrumentID) where T : IInstrumentStaticData
        {
            return (T)this.GetInstrumentStaticDataFromCache(instrumentID);
        }

        /// <summary>
        /// The get instrument.
        /// </summary>
        /// <param name="instrumentID">
        /// The instrumentID.
        /// </param>
        /// <returns>
        /// </returns>
        public IInstrumentStaticData GetInstrumentStaticDataFromCache(Guid instrumentID)
        {
#if BENCHMARKDAOINSTRUMENTS
            var v0 = Timings.BenchmarkNow;
#endif
            if (!this.instrumentsCache.ContainsKey(instrumentID))
            {
                var i = DBInstrumentStaticDataHelpers.GetInstrumentStaticData<IInstrumentStaticData>(instrumentID);
                if (i != null)
                {
                    this.instrumentsCache[instrumentID] = i;
                }

                return i;
            }

            var instrument = this.instrumentsCache[instrumentID];

#if BENCHMARKDAOINSTRUMENTS

    // Benchmarking
            Timings.Benchmark("GetInstrumentStaticDataFromCache", v0);
#endif
            return instrument;
        }

        /// <summary>
        /// The save instrument.
        /// </summary>
        /// <param name="i">
        /// The i.
        /// </param>
        /// <returns>
        /// The save instrument.
        /// </returns>
        public bool SaveInstrumentStaticDataAndCache(IInstrumentStaticData i)
        {
#if BENCHMARKDAOINSTRUMENTS
            var v0 = Timings.BenchmarkNow;
#endif
            this.instrumentsCache[i.ID] = i;
            var success = DBInstrumentStaticDataHelpers.SaveInstrumentStaticDatas(i);

#if BENCHMARKDAOINSTRUMENTS

    // Benchmarking
            Timings.Benchmark("SaveInstrumentStaticDataAndCache", v0);
#endif
            return success;
        }

        /// <summary>
        /// The save instrument wirings.
        /// </summary>
        /// <param name="i">
        /// The i.
        /// </param>
        public void SaveInstrumentStaticDataWirings(IInstrumentStaticData i)
        {
#if BENCHMARKDAOINSTRUMENTS
            var v0 = Timings.BenchmarkNow;
#endif
            this.instrumentsCache[i.ID] = i;
            DBInstrumentStaticDataHelpers.SaveInstrumentStaticDataWirings(i);

#if BENCHMARKDAOINSTRUMENTS

    // Benchmarking
            Timings.Benchmark("SaveInstrumentStaticDataWiringsAndCache", v0);
#endif
        }

        #endregion
    }
}